﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace Axe.Extensions
{
    public static class MethodExtensions
    {
        public static T Cast<T>(this object obj)
        {
            return (T)obj;
        }


        public static void Add(this ControlCollection ctrl, params Control[] controls)
        {
            foreach (Control c in controls)
            {
                ctrl.Add(c);
            }
        }

        /// <summary>
        /// Удобный Extension для работы с коллекцией ViewState
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="viewState"></param>
        /// <param name="name"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static T Get<T>(this StateBag viewState,string name, T defaultValue)
        {
            object o = viewState[name];
            if (o == null)
            {
                viewState[name] = defaultValue;
                return defaultValue;
            }

            if (o is T)
                return (T)o;
            else
            {
                viewState[name] = defaultValue;
                return defaultValue;
            }
        }

        public static T Get<T>(this StateBag viewState, string name)
        {
            T defaultValue = default(T);

            object o = viewState[name];
            if (o == null)
                return defaultValue;

            if (o is T)
                return (T)o;
            else
                return defaultValue;
        }
    }
}
